package org.esprit.ocm.client.ui.common;

import org.esprit.ocm.client.rpc.AwsCredentialsService;
import org.esprit.ocm.client.rpc.AwsCredentialsServiceAsync;
import org.esprit.ocm.client.rpc.ServerService;
import org.esprit.ocm.client.rpc.ServerServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class ServerAccount extends Window {

	private final ServerServiceAsync			serverService		= GWT.create(ServerService.class);
	private final AwsCredentialsServiceAsync	credentialsService	= GWT.create(AwsCredentialsService.class);

	public ServerAccount(final UserDto u, final String type) {
		setWidth(300);
		setHeight(350);
		setTitle("Account Window");
		setShowMinimizeButton(false);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		centerInPage();
		addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClientEvent event) {
				destroy();
			}
		});

		final DynamicForm formAccount = new DynamicForm();
		formAccount.setWidth(250);
		formAccount.setHeight(150);
		formAccount.setPadding(5);
		formAccount.setLayoutAlign(VerticalAlignment.TOP);

		final TextItem serverName = new TextItem("serverName");
		serverName.setTitle("Server Name");
		serverName.setRequired(true);

		final ComboBoxItem typeCB = new ComboBoxItem();
		typeCB.setTitle("<b>Type:<b>");
		if (type.equals("one")) {
			typeCB.setDefaultValue("oca");
		} else if (type.equals("ec2")) {
			typeCB.setDefaultValue("ec2");
		} else if (type.equals("s3")) {
			typeCB.setDefaultValue("s3");
		}
		typeCB.setValueMap("oca", "ec2", "s3");

		final TextItem ipServer = new TextItem("ipServer");
		ipServer.setTitle("Server Ip");
		ipServer.setRequired(true);

		final TextItem port = new TextItem("port");
		port.setTitle("Port");
		port.setRequired(true);

		final RadioGroupItem httpsRG = new RadioGroupItem();
		httpsRG.setName("HTTPS");
		httpsRG.setRequired(true);
		httpsRG.setVertical(false);
		httpsRG.setDefaultValue("false");
		httpsRG.setValueMap("true", "false");

		final TextItem resourcePrefix = new TextItem("resourcePrefix");
		resourcePrefix.setTitle("Resource Prefix");
		resourcePrefix.setDefaultValue(" ");

		final TextItem signatureVersion = new TextItem("signatureVersion");
		signatureVersion.setTitle("Signature Version");
		signatureVersion.setDefaultValue("2");

		final TextItem accessKey = new TextItem("accessKey");
		accessKey.setTitle("Access Key");
		accessKey.setRequired(true);

		final PasswordItem secretKey = new PasswordItem("secretKey");
		secretKey.setTitle("Secret Key");
		secretKey.setRequired(true);

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				ServerDto serv = new ServerDto();
				serv.setIpServer(ipServer.getValue().toString());
				serv.setNameServer(serverName.getValue().toString());
				serv.setPort(Integer.parseInt(port.getValue().toString()));
				serv.setResourcePrefix(resourcePrefix.getValue().toString());
				serv.setSignatureVersion(Integer.parseInt(signatureVersion.getValue().toString()));
				serv.setTypeServer(typeCB.getValue().toString());
				serv.setUseHTTPS(Boolean.parseBoolean(httpsRG.getValue().toString()));
				serv.setUser(u);
				System.out.println("-" + serv.getIdServer() + "-" + serv.getUser().getIdUser());
				serverService.add(serv, new AsyncCallback<ServerDto>() {

					public void onSuccess(ServerDto s) {

						System.out.println(s.getIdServer() + "-" + s.getUser().getIdUser());
						AwsCredentialsDto cred = new AwsCredentialsDto();
						cred.setAwsAccessKeyId(accessKey.getValue().toString());
						cred.setSecretAccessKey(secretKey.getValue().toString());
						cred.setServer(s);

						credentialsService.add(cred, new AsyncCallback<AwsCredentialsDto>() {

							public void onSuccess(AwsCredentialsDto c) {

								SC.say("Server created");
								destroy();
								ServerWindow serverWindow = new ServerWindow(u);
								serverWindow.show();
							}

							public void onFailure(Throwable arg0) {
								// TODO Auto-generated method stub

							}
						});
					}

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub

					}
				});
			}
		});
		
		ButtonItem closeButton = new ButtonItem("close", "Close");
		closeButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				destroy();
				ServerWindow serverWindow = new ServerWindow(u);
				serverWindow.show();
			}
		});

		formAccount.setFields(serverName, typeCB, ipServer, port, httpsRG, resourcePrefix, signatureVersion, accessKey, secretKey, createButton, closeButton);
		formAccount.redraw();

		addItem(formAccount);
	}
}
